//
//  Mine2ViewController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 16/4/11.
//  Copyright © 2016年 sgone. All rights reserved.

//

#import "Mine2ViewController.h"
#import "DoorMessage.h"
#import "XTTZController.h"
#import "XTTZ2Controller.h"
#import "MJRefresh.h"
#import "MyHomeTableViewCell2.h"
#import "PurchaseViewController.h"
#import "YYTJController.h"
#import "ZHAQController.h"
#import "TXController.h"
#import "DDHZController.h"
#import "AFNetworking.h"
#import "YYTJ2ViewController.h"
#import "SetViewController.h"


#define height1 10
@interface Mine2ViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    UIView *titleView;
    UILabel *titleName;
    UIButton *noticeBtn;
    UIButton *setBtn;
    UILabel * numbeLbl;
    UITableView *tableView1;
    
    MyHomeTableViewCell2 *cell2;
    NSArray *arrayImage;
    NSArray *arrayName;
    

    UILabel *lblNum1;
    UILabel *lblNum2;
    UILabel *lblNum3;
    NSString *value;
    UIButton *currentBtn;
    UIButton *btnJ;
    UIButton *btnQ;
    UIButton *btnM;
    
}

@property(nonatomic,strong) AFHTTPSessionManager *manager;
@end

@implementation Mine2ViewController


//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}


-(void)viewWillAppear:(BOOL)animated
{

    [self NoticeNumber];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToHomeClick:) name:@"goToHomeClick" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NoticeNumber) name:@"NoticeNum2" object:nil];
    
    
    arrayName = @[@"货品管理",@"营业统计",@"账户设置",@"通知",@"设置",@""];
    arrayImage = @[@"m1",@"m2",@"m3",@"m4",@"m5",@""];

    self.topView.hidden = YES;
    [self creatTitleView];
    [self creatTableView];

    
    
    [self setupRefresh];
 
//    dispatch_async(dispatch_get_main_queue(), ^{
//        
//        [self NoticeNumber];
//        
//    });
    
    
}


-(void)creatTitleView
{

    self.view.backgroundColor = BACKGROUND1;
    titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, 20+100/FDLaOt)];
    titleView.backgroundColor = BACKGROUND1;
    [self.view addSubview:titleView];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:titleView.frame];
    imageView.image = [UIImage imageNamed:@"我的背景"];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [titleView addSubview:imageView];
   
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = [UIColor whiteColor];
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica-Blod" size:18.0f]];
    titleLabel.frame = CGRectMake(16, 26, ViewWidth - 16, 21);
    titleLabel.text = @"批发商:";
    [titleView addSubview:titleLabel];
    
    
    
    titleName = [[UILabel alloc]initWithFrame:CGRectMake(titleLabel.frame.origin.x + 10, titleLabel.frame.origin.y + titleLabel.frame.size.height, ViewWidth - 22, 21)];
    [titleView addSubview:titleName];
    [titleName setFont:[UIFont fontWithName:@"Helvetica-Blod" size:15.0f]];
    titleName.text = [NSString stringWithFormat:@"%@",self.dataHandler.name];
    titleName.textColor = [UIColor whiteColor];
    
    
    noticeBtn = [[UIButton alloc]initWithFrame:CGRectMake(ViewWidth - 50 , 26, 22, 22)];
    noticeBtn.backgroundColor = [UIColor clearColor];
    [noticeBtn setImage:[UIImage imageNamed:@"ling"] forState:UIControlStateNormal];
    [titleView addSubview:noticeBtn];
    [noticeBtn addTarget:self action:@selector(notice) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    numbeLbl = [[UILabel alloc]initWithFrame:CGRectMake(noticeBtn.frame.origin.x + noticeBtn.frame.size.width - 10, 22, 14 /FDLaOt, 14/FDLaOt)];
    numbeLbl.layer.masksToBounds = YES;
    numbeLbl.layer.cornerRadius = 7 / FDLaOt;
    [titleView addSubview:numbeLbl];
    numbeLbl.font = [UIFont systemFontOfSize:10/FDLaOt];
    numbeLbl.backgroundColor = [UIColor whiteColor];
    numbeLbl.textAlignment = NSTextAlignmentCenter;
    numbeLbl.textColor = BenSe_COLOR;
    numbeLbl.hidden = YES;
    
    
    
    UIButton *storeBtn = [[UIButton alloc] initWithFrame:CGRectMake(ViewWidth - 80 - 16, titleView.height - 25, 80, 20)];
    [storeBtn setTitle:@"店铺设置" forState:UIControlStateNormal];
    [storeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    storeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    storeBtn.titleLabel.font = [UIFont systemFontOfSize:13.0f];
    [storeBtn addTarget:self action:@selector(storeClick) forControlEvents:UIControlEventTouchUpInside];

    [titleView addSubview:storeBtn];

}



#pragma mark 店铺设置
-(void)storeClick
{
    DoorMessage *DM = [[DoorMessage alloc]initWithNibName:nil bundle:nil];
    [DM setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:DM animated:YES];

}



-(void)NoticeNumber
{

    NSString *urlStr = [NSString stringWithFormat:@"%@no_chart_log_read",SERVER_ADDRESS3];
    NSDictionary *parmas = @{@ "uid" : [self.dataHandler.userDic objectForKey:@"wId"]  ,@"user_type" : @"3" , @"chart_type"  : @"2"};
    [self.manager POST:urlStr parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        
        
    
        
        
        if ([responseObject[@"error_flag"] intValue] == 0) {
           
            numbeLbl.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"list_count"]];
    
            
            if([numbeLbl.text intValue] == 0)
            {
                numbeLbl.hidden = YES;
            
            }
            else
            {
                numbeLbl.hidden = NO;
            
            }

        }
        
        else
        {
        
        
        
        
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
       
        
        
        
        
    }];
    
    



}

#pragma mrak 通知消息
-(void)notice
{
    XTTZController *tz  = [[XTTZController alloc]initWithNibName:nil bundle:nil];
    [tz setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:tz animated:YES];
}


-(void)creatTableView
{

    tableView1 = [[UITableView alloc]initWithFrame:CGRectMake(0,  titleView.frame.size.height, ViewWidth, ViewHeight - titleView.frame.size.height - 44) style:UITableViewStyleGrouped];
    tableView1.backgroundColor = BACKGROUND1;
    [self.view addSubview:tableView1];
    tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView1.delegate = self;
    tableView1.dataSource = self;
    
 
}



#pragma mark刷新数据
- (void)setupRefresh {
    
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [tableView1 addHeaderWithTarget:self action:@selector(headerRereshing)];
    
    //#warning 自动刷新(一进入程序就下拉刷新)
    [tableView1 headerBeginRefreshing];
    
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    //    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing13)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    tableView1.headerPullToRefreshText = @"下拉可以刷新了";
    tableView1.headerReleaseToRefreshText = @"松开马上刷新了";
    tableView1.headerRefreshingText = @"正在帮你刷新中";

}
- (void)headerRereshing {
    
    [self getDate];
}



- (void)getDate {
    
    
    value = @"day";
    
    if (btnJ ) {
        
        btnJ.selected = YES;
        currentBtn = btnJ;
        btnQ.selected = NO;
        btnM.selected = NO;
    }

    
    
    [self sendAPI];
//    [tableView1 headerEndRefreshing];
    
    
    
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    return 90 /FDLaOt +10 +10;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100 / FDLaOt;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    
   return   [self contentView];
 
}




-(UIView *)contentView
{

    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, height1)];
    contentView.backgroundColor = [UIColor whiteColor];
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, height1)];
    view2.backgroundColor = BACKGROUND1;
    [contentView addSubview:view2];
    
    
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, height1, ViewWidth, 0.5)];
    lbl.backgroundColor = FDLine_COLOR;
    [contentView addSubview:lbl];
    
    
    
    
    UILabel *lbl1 =  [[UILabel alloc]initWithFrame:CGRectMake(10 / FDLaOt, height1, 80, 40/FDLaOt)];
    lbl1.text = @"我的订单";
    lbl1.textAlignment = NSTextAlignmentLeft;
    lbl1.font = [UIFont systemFontOfSize:13/FDLaOt];
    [contentView addSubview:lbl1];

    
    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/4 - 8, height1 + 10 / FDLaOt, 1, 20/FDLaOt)];
    lbl2.backgroundColor = BACKGROUND1;
    [contentView addSubview:lbl2];
    
    
    
    btnJ = [[UIButton alloc]initWithFrame:CGRectMake(ViewWidth / 4 -8, height1, (ViewWidth / 4) / 2 + 8, ViewWidth / 4 / 2 )];
    [btnJ setTitle:@"今日" forState:UIControlStateNormal];
    [btnJ setTitleColor:BenSe_COLOR forState:UIControlStateSelected];
    [btnJ setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [contentView addSubview:btnJ];
    btnJ.titleLabel.textAlignment = NSTextAlignmentCenter;
    btnJ.titleLabel.font = [UIFont systemFontOfSize:10/FDLaOt];
    btnJ.tag = 1;
    [btnJ addTarget:self action:@selector(day:) forControlEvents:UIControlEventTouchUpInside];
    btnJ.selected = YES;
    currentBtn = btnJ;
    
    
    
    UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(btnJ.frame.origin.x + btnJ.frame.size.width , height1 + 10 / FDLaOt, 1, 20/FDLaOt)];
    lbl3.backgroundColor = BACKGROUND1;
    [contentView addSubview:lbl3];
    
    
    
   btnQ = [[UIButton alloc]initWithFrame:CGRectMake(lbl3.frame.origin.x + lbl3.frame.size.width   , height1, (ViewWidth / 4) / 2 + 16 , 40/FDLaOt)];
    [btnQ setTitle:@"近7天" forState:UIControlStateNormal];
    [btnQ setTitleColor:BenSe_COLOR forState:UIControlStateSelected];
    [btnQ setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btnQ.titleLabel.textAlignment = NSTextAlignmentCenter;
    btnQ.titleLabel.font = [UIFont systemFontOfSize:10/FDLaOt];
    [contentView addSubview:btnQ];
    btnQ.tag = 2;
    [btnQ addTarget:self action:@selector(day:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    UILabel *lbl4 = [[UILabel alloc]initWithFrame:CGRectMake(btnQ.frame.origin.x + btnQ.frame.size.width , height1 + 10 / FDLaOt, 1, 20/FDLaOt)];
    lbl4.backgroundColor = BACKGROUND1;
    [contentView addSubview:lbl4];
    
    btnM = [[UIButton alloc]initWithFrame:CGRectMake(lbl4.frame.origin.x + lbl4.frame.size.width   , height1, (ViewWidth / 4) / 2 + 16 , 40/FDLaOt)];
    [btnM setTitle:@"近30天" forState:UIControlStateNormal];
    [btnM setTitleColor:BenSe_COLOR forState:UIControlStateSelected];
    [btnM setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btnM.titleLabel.textAlignment = NSTextAlignmentCenter;
    btnM.titleLabel.font = [UIFont systemFontOfSize:10/FDLaOt];
    [contentView addSubview:btnM];
    btnM.tag = 3;
    [btnM addTarget:self action:@selector(day:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    UIButton *btnDD = [[UIButton alloc]initWithFrame:CGRectMake(ViewWidth - 115 , height1   , 110, 40 / FDLaOt)];
    [btnDD setTitle:@"查看全部订单" forState:UIControlStateNormal];
    [btnDD setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [contentView addSubview:btnDD];
    btnDD.titleLabel.textAlignment = NSTextAlignmentLeft;


    
    
    UIImageView *image2 = [[UIImageView alloc]init];
                           

    
    if ([UIScreen mainScreen].bounds.size.width  == 320.0) {
        
        image2.frame = CGRectMake(btnDD.frame.origin.x + btnDD.frame.size.width - 12, height1 + 14/FDLaOt, 8, 10);
    }
    else
    {
    
        image2.frame = CGRectMake(btnDD.frame.origin.x + btnDD.frame.size.width - 12, height1 + 16/FDLaOt, 8, 10);
    }
    
    
    image2.image = [UIImage imageNamed:@"JD"];
    [contentView addSubview:image2];
    
    
//    [btnDD setImage:[UIImage imageNamed:@"JD"] forState:UIControlStateNormal];
//    
//
//    //图片放置
//    
//    btnDD.titleEdgeInsets = UIEdgeInsetsMake(0, -btnDD.imageView.frame.size.width - btnDD.frame.size.width + btnDD.titleLabel.intrinsicContentSize.width ,0 ,0);
//    btnDD.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -btnDD.titleLabel.frame.size.width - btnDD.frame.size.width + btnDD.imageView.frame.size.width);
    
    
    
    btnDD.titleLabel.font = [UIFont systemFontOfSize:11/FDLaOt];
    [btnDD addTarget:self action:@selector(DD:) forControlEvents:UIControlEventTouchUpInside];

    
    
    
    UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(12, height1 + 40 / FDLaOt, ViewWidth - 24, 1)];
    view4.backgroundColor = BACKGROUND1;
    [contentView addSubview:view4];
    
    
    
    
    for (int i = 0; i < 2; i++) {
        
        UILabel *lbl11 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/3 * (i+1) , height1 / FDLaOt + 40 / FDLaOt + 10/ FDLaOt , 1, 30 / FDLaOt )];
        lbl11.backgroundColor = BACKGROUND1;
        [contentView addSubview:lbl11];
        
        
    }
    

    
    
    for (int i = 0; i < 3; i++) {
        
        UILabel *lbl12 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/3 * i, height1 / FDLaOt + 40 / FDLaOt + 28/ FDLaOt, ViewWidth / 3, 21)];
        lbl12.textColor = [UIColor grayColor];
        [contentView addSubview:lbl12];
        lbl12.font = [UIFont systemFontOfSize:12/FDLaOt];
        lbl12.textAlignment = NSTextAlignmentCenter;
        
        switch (i) {
            case  0:
                lbl12.text = @"新订单";
              break;
            case 1:
                lbl12.text = @"待发货";
                break;
            case 2:
                lbl12.text = @"已完成";
                break;
          
                
                
            default:
                break;
        }
        
    }
    
    
    
 
    
    
    lblNum1 = [[UILabel alloc]initWithFrame:CGRectMake(0 , height1 / FDLaOt + 40 / FDLaOt + 6/ FDLaOt, ViewWidth / 3, 21)];
    lblNum1.textColor = BenSe_COLOR;
    [contentView addSubview:lblNum1];
    lblNum1.font = [UIFont systemFontOfSize:12/FDLaOt];
    lblNum1.textAlignment = NSTextAlignmentCenter;
    lblNum1.tag = 110;
    
    
    
    lblNum2 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/3 , height1 / FDLaOt + 40 / FDLaOt + 6/ FDLaOt, ViewWidth / 3, 21)];
    lblNum2.textColor = BenSe_COLOR;
    [contentView addSubview:lblNum2];
    lblNum2.font = [UIFont systemFontOfSize:12/FDLaOt];
    lblNum2.textAlignment = NSTextAlignmentCenter;
    lblNum2.tag = 111;
    
    
    
    
    lblNum3 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/3 *2 , height1 / FDLaOt + 40 / FDLaOt + 6/ FDLaOt, ViewWidth / 3, 21)];
    lblNum3.textColor = BenSe_COLOR;
    [contentView addSubview:lblNum3];
    lblNum3.font = [UIFont systemFontOfSize:12/FDLaOt];
    lblNum3.textAlignment = NSTextAlignmentCenter;
    lblNum3.tag = 112;

    
    
    
    UILabel *lbl33 = [[UILabel alloc]initWithFrame:CGRectMake(0, 99.5/FDLaOt, ViewWidth, 0.5/FDLaOt)];
    lbl33.backgroundColor = FDLine_COLOR;
    [contentView addSubview:lbl33];

    
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 100 /FDLaOt, ViewWidth, 10 )];
    view3.backgroundColor = BACKGROUND1;
    [contentView addSubview:view3];
    
    
    
    UILabel *lbl44 = [[UILabel alloc]initWithFrame:CGRectMake(0, 100/FDLaOt + 9.5, ViewWidth, 0.5)];
    lbl44.backgroundColor = FDLine_COLOR;
    [contentView addSubview:lbl44];


    return contentView;

}


-(void)day:(UIButton *)sender
{
   
    
    
    currentBtn.selected = NO;
    sender.selected = YES;
    currentBtn = sender;
    
    if (sender.tag == 1) {
        
        value = @"day";
    }
    else if (sender.tag == 2)
    {
    
        value = @"week";
    
    }
    else if (sender.tag == 3)
    {
    
    
        value = @"month";
    
    
    }
    
    [self sendAPI];
   
}


-(void)DD:(id)sender
{

    self.tabBarController.selectedIndex = 1;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    cell2 = [MyHomeTableViewCell2 cellWithTableView:tableView];
    cell2.selectionStyle = UITableViewCellSelectionStyleNone;
    cell2.row = (NSInteger *)indexPath.row;
    
    cell2.array1 = arrayImage;
    cell2.array2 = arrayName;
    [cell2 setMyCell];

    return cell2;
}



- (void)goToHomeClick:(id)sender
{
    
    NSNotification *user = (NSNotification *)sender;
    NSDictionary *dic = (NSDictionary *)user.userInfo;
    
     if ([dic[@"row"] integerValue] == 0 && [dic[@"tag"] integerValue] == 0) {

        
         
         self.tabBarController.selectedIndex = 2;
        
    }
    else if ([dic[@"row"] integerValue] == 0 && [dic[@"tag"] integerValue] == 1)
    {
        
        YYTJ2ViewController *yytj = [[YYTJ2ViewController alloc] initWithNibName:nil bundle:nil];
        
        [yytj setHidesBottomBarWhenPushed:YES];
        
        [self.navigationController pushViewController:yytj animated:YES];
    
    }
    else if  ([dic[@"row"] integerValue] == 0 && [dic[@"tag"] integerValue] == 2)
    {
 
        
        
        TXController *tx = [[TXController alloc]initWithNibName:nil bundle:nil];
        
        
        [tx setHidesBottomBarWhenPushed:YES];
        
        
        [self.navigationController  pushViewController:tx animated:YES];
    
    
    }
    else if ([dic[@"row"] integerValue] == 1 && [dic[@"tag"] integerValue] == 0)
    {
        
        
        XTTZ2Controller * tz = [[XTTZ2Controller alloc]initWithNibName:nil bundle:nil];
        [tz setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:tz animated:YES];
        
    
    
    }
    else if ([dic[@"row"] integerValue] == 1 && [dic[@"tag"] integerValue] == 1)
    {
       
//        ZHAQController *zhaq = [[ZHAQController alloc]initWithNibName:nil bundle:nil];
    
        
        SetViewController *set = [[SetViewController alloc]initWithNibName:nil bundle:nil];
        [set setHidesBottomBarWhenPushed:YES];
        
        
        
        [self.navigationController pushViewController:set animated:YES];
    
    }
    else
    {
    
        return;
    
    }


}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)sendAPI
{

    lblNum1.text = @"";
    lblNum2.text = @"";
    lblNum3.text = @"";

    
  
    NSDictionary *pramas =@{ @"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"type" : value};
    NSString *url = [NSString stringWithFormat:@"%@get_order_report",SERVER_ADDRESS2];
    
   
    [self.manager POST:url parameters:pramas success:^(NSURLSessionDataTask *task, id responseObject) {
        

        if ([responseObject[@"error_flag"] intValue] == 0) {
            
            lblNum1.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"list"][@"order_new"]];
            lblNum2.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"list"][@"order_send"]];
            lblNum3.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"list"][@"order_complete"]];

            NSLog(@"asdasdasd %@",value);
     
            [tableView1 headerEndRefreshing];

        }
        else
        {
        
            NSString * str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            [self.view makeToast:str];
            [tableView1 headerEndRefreshing];
        
        
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
     
        [self.view makeToast:@"请检查网络!"];
        [tableView1 headerEndRefreshing];
        
    }];




}

@end
